package com.wholeintech.zigbee.gw;

import android.nfc.Tag;
import android.util.Log;

import com.wholeintech.utils.LogUtils;
import com.wholeintech.utils.CommonUtil;
import com.wholeintech.entity.DeviceDb;

import static com.wholeintech.utils.ByteUtils.toHexString;
import static com.wholeintech.zigbee.gw.DeviceManager.DEVICE_ZIGBEE_DEVID_LEN;
import static com.wholeintech.zigbee.gw.DeviceManager.DEVICE_ZIGBEE_LIGHT8_SUB_DEVID_LEN;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_AIRCON;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_CURTAIN;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_FLOOR_HEATING;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_FRESH_AIR;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_LIGHT;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_LIGHT_8;
import static com.wholeintech.zigbee.gw.DeviceManager.TYPE_LIGHT_DIMMER;

/**
 * Zigbee设备控制类
 */
public class DeviceControl {
	private final String TAG = "DeviceControl";
	private ZigBeeDataProcess mZBprocess;
	private static DeviceControl mDeviceControl;

	private DeviceControl(ZigBeeDataProcess m) {
		mZBprocess = m;
	}

	public static DeviceControl getInstance(ZigBeeDataProcess m) {
		if (mDeviceControl == null) {
			mDeviceControl = new DeviceControl(m);
		}
		return mDeviceControl;
	}

	/**
	 * 灯控制
	 * @param mac
	 * @param sw
	 * @param brightness
	 * @param color
	 */
	public void light(String mac, int sw, int brightness, int color) {
		byte[] data = new byte[4];
		byte[] macbyte = CommonUtil.macbytes(mac);
		if (sw != 0)
			data[0] = 1;
		else
			data[0] = 0;
		if (brightness > 255)
			data[1] = (byte) 255;
		else
			data[1] = (byte) brightness;
		data[2] = (byte) color;
		data[3] = (byte) (color >> 8);
		byte[] sendData = CommonUtil.mergebyte(macbyte, data);
		mZBprocess.TxPackDataBuffer((byte) DeviceManager.UART_CMD_eControl,
				sendData);

		LogUtils.d("DeviceControl","====control=light==command:"+toHexString(sendData)+"===sw:"+sw);
		//Log.e(TAG,"====control=light==command:"+toHexString(sendData)+"===sw:"+sw);
	}

	/**
	 * 一拖8灯控制
	 * @param mac
	 * @param sw
	 * @param brightness
	 * @param color
	 */
	public void light8(String mac, int sw, int brightness, int color) {
		byte[] data = new byte[4];
		byte[] macbyte = CommonUtil.macbytes(mac);

		data[0] = (byte)(sw);

		if (brightness > 255)
			data[1] = (byte) 255;
		else
			data[1] = (byte) brightness;
		data[2] = (byte) (color);//MCU low byte
		data[3] = (byte) (color >> 8);//MCU high byte
		byte[] sendData = CommonUtil.mergebyte(macbyte, data);
		mZBprocess.TxPackDataBuffer((byte) DeviceManager.UART_CMD_eControl,
				sendData);
		Log.e("HwService","====DeviceControl==ctrlLight8==cmd:"+toHexString(sendData));
		LogUtils.d("DeviceControl","====control=light8==command:"+toHexString(sendData));
	}

	/**
	 * 窗帘控制
	 * @param mac
	 * @param runstate
	 * @param runstate2
	 */
	public void curtain(String mac, int runstate, int runstate2) {
		byte[] data = new byte[2];
		byte[] macbyte = CommonUtil.macbytes(mac);

		data[0] = (byte) runstate;
		data[1] = (byte) runstate2;

		byte[] sendData = CommonUtil.mergebyte(macbyte, data);
		mZBprocess.TxPackDataBuffer((byte) DeviceManager.UART_CMD_eControl, sendData);
		LogUtils.d("DeviceControl","====control=curtain==command:"+toHexString(sendData));
	}

	/**
	 * 空调控制
	 * @param mac
	 * @param sw
	 * @param mode
	 * @param temp
	 * @param wind
	 */
	public void airCondiction(String mac, int sw, int mode, int temp, int wind) {
		byte[] data = new byte[4];
		byte[] macbyte = CommonUtil.macbytes(mac);

		data[0] = (byte) sw;
		data[1] = (byte) mode;
		data[2] = (byte) temp;
		data[3] = (byte) wind;

		byte[] sendData = CommonUtil.mergebyte(macbyte, data);
		mZBprocess.TxPackDataBuffer((byte) DeviceManager.UART_CMD_eControl, sendData);
		LogUtils.d("DeviceControl","====control=airCondiction==command:"+toHexString(sendData));
	}

	/**
	 * 新风控制
	 * @param mac
	 * @param sw
	 * @param wind
	 * @param mode
	 * @param ext
	 */
	public void freshAir(String mac, int sw, int wind, int mode, int ext) {
		byte[] data = new byte[4];
		byte[] macbyte = CommonUtil.macbytes(mac);
		data[0] = (byte) sw;
		data[1] = (byte) wind;
		data[2] = (byte) mode;
		data[3] = (byte) ext;
		byte[] sendData = CommonUtil.mergebyte(macbyte, data);
		mZBprocess.TxPackDataBuffer((byte) DeviceManager.UART_CMD_eControl, sendData);
		LogUtils.d("DeviceControl","====control=freshAir==command:"+toHexString(sendData));
	}

	public static void control(DeviceDb deviceDb) {
		LogUtils.d("DeviceControl","====control==devId:"+deviceDb.getDevId()+"==sw:"+deviceDb.getSw()+"==boxId:"+deviceDb.getBoxId()+"==name:"+deviceDb.getName()+"==type:"+deviceDb.getType()+"==status:"+deviceDb.getStatus()+"==");
		try {
	    	if(deviceDb!=null && deviceDb.getGwDevId()!=null){
				if(deviceDb.getGwDevId().length()==DEVICE_ZIGBEE_DEVID_LEN &&
						deviceDb.getDevId().length() != DEVICE_ZIGBEE_LIGHT8_SUB_DEVID_LEN){
					return;
				}
	        }
	    	
			switch (deviceDb.getType()) {
				case TYPE_LIGHT:
				case TYPE_LIGHT_DIMMER:
					mDeviceControl.light(deviceDb.getMacAddr(), deviceDb.getSw(), deviceDb.getBrightness(), deviceDb.getColor());
					Log.e("peng_test","==TYPE_LIGHT==name:"+deviceDb.getName()+"==sw:"+deviceDb.getSw());
					break;
				case TYPE_LIGHT_8:
					mDeviceControl.light8(deviceDb.getMacAddr(), deviceDb.getSw(), deviceDb.getBrightness(), deviceDb.getColor());
					break;
				case TYPE_CURTAIN:
					mDeviceControl.curtain(deviceDb.getMacAddr(), deviceDb.getCurtainState(), deviceDb.getCurtainState2());
					break;
				case TYPE_FLOOR_HEATING:
				case TYPE_AIRCON:
					mDeviceControl.airCondiction(deviceDb.getMacAddr(), deviceDb.getSw(), deviceDb.getMode(), deviceDb.getSetTemp(), deviceDb.getWind());
					break;
				case TYPE_FRESH_AIR:
					mDeviceControl.freshAir(deviceDb.getMacAddr(), deviceDb.getSw(), deviceDb.getWind(), deviceDb.getMode(), deviceDb.getCurtainState());
					break;
				default:
					break;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
